<?php

namespace Home\Controller;

use Think\Controller;

class BoxController extends Controller {
    public function getMap() {
        $lat = I("post.lat");
        $lng = I("post.lng");
        echo mt_rand(0,5);exit;
        $arr = array("status"=>"ok","count"=>'1234');
        $round = 100;
           $rs =   GetRange($lat,$lng,$round);
        
//        $x_max = $lat+$round;
//        $x_min = $lat-$round;
//        $y_max = $lng+$round;
//        $y_min = $lng-$round;
         $x_max = $rs['maxLat'];
        $x_min = $rs['minLat'];
        $y_max = $rs['maxLon'];
        $y_min = $rs['minLon'];
  
//     print_r($rs);
//        SELECT * From 店铺 WHERE  (x>=x2 And X<=x1) And (y>=x2 And y<=y1)
        $num = M("shop")->where("map_x >= '".$x_min."' AND map_x <= '".$x_max."' AND "
                . "map_y >= '".$y_min."' AND map_y <= '".$y_max."'")->count();
      // echo M("shop")->getlastsql();
       $arr['count'] = $num;
echo json_encode($arr);

    }
    public function addAddr() {
        $this->assign("addr", I("post.addr"));
        $this->assign("phone", I("post.phone"));
        $this->assign("phone2", I("post.phone2"));
        $this->assign("truename", I("post.truename"));
        $this->assign("id", I("post.id"));
        if (I("post.mtype") == 'home') {
            $this->display("Box:addAddr2");
        } else {
            $this->display();
        }
    }

    public function saveAddr() {
        $id = I("post.id");
        $data['truename'] = I("post.truename");
        $data['phone'] = I("post.phone");
        $data['phone2'] = I("post.phone2");
        $data['addr'] = I("post.addr");
        $uid = session("userid");
        if ($id > 0) {
            M('addr')->where("id = " . $id . " AND uid = " . session("userid") . "")->save($data);
        } else {
            $data['uid'] = $uid;
            $lastid = M('addr')->add($data);
            echo $lastid;
        }
    }

    public function delAddr() {
        $id = I("post.id", 0, 'int');
        M('addr')->where("id = " . $id . " AND uid = " . session("userid") . "")->delete();
    }

    public function setAddr() {
        $id = I("post.id");
        M('addr')->where("uid = " . session("userid") . " AND id != " . $id . "")->save(array("is_default" => 0));
        M('addr')->where("id = " . $id . " AND uid = " . session("userid") . "")->save(array("is_default" => 1));
    }

    public function comment() {
        $id = I("post.id");
        $order = M("order")->field("sid,sname,state")->where("id = '" . $id . "' AND uid = " . session("userid") . "")->find();
        $this->assign("order", $order);
        $this->assign("id", $id);
        $this->display();
    }
 

}
